package com.leetcode.LC_MED;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LC17 {
    public List<String> letterCombinations(String digits) {

        ArrayList<String> list = new ArrayList<>();
        if (digits.length() == 0) {
            return list;
        }

        Map<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");
        backTrack(list, map, digits, 0, new StringBuilder());
        return list;
    }

    private void backTrack(List<String> list, Map<Character, String> map, String digits, int index, StringBuilder com) {

        if (com.length() == digits.length()) {
            list.add(com.toString());
            return;
        }

        char digit = digits.charAt(index);
        String s = map.get(digit);
        int length = s.length();

        for (int i = 0; i < length; i++) {
            com.append(s.charAt(i));
            backTrack(list, map, digits, index + 1, com);
            com.deleteCharAt(index);
        }

    }
}
